package com.uenpay.im.ui.old.activity.bankcard;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.im.R;

import java.util.ArrayList;

/**
 * @Author: SL
 * @Date: 2017/1/17 13:18
 * @CopyRight: http://www.qiyunxin.com
 * @Parameter: TODO
 * @Function: TODO
 */
public class ChooseBankAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<BankEntity> arrayList;

    public ChooseBankAdapter(Context _Context, ArrayList<BankEntity> _arrayList) {
        mContext = _Context;
        arrayList = _arrayList;
    }

    @Override
    public int getCount() {
        return arrayList == null ? 0 : arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList == null ? null : arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BankViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new BankViewHolder();
            convertView = View.inflate(mContext, R.layout.item_choose_bank_layout, null);
            viewHolder.bankName = (TextView) convertView.findViewById(R.id.bank_name_tv);
            viewHolder.accountName = (TextView) convertView.findViewById(R.id.bank_account_tv);
            viewHolder.selectedIv = (ImageView) convertView.findViewById(R.id.selected_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (BankViewHolder) convertView.getTag();
        }
        viewHolder.bankName.setText(arrayList.get(position).bank_name);
        viewHolder.accountName.setText(mContext.getResources().getString(R.string.bankc_card_end_num) + arrayList.get(position).card_no.substring(arrayList.get(position).card_no.length() - 5, arrayList.get(position).card_no.length()));
        if (arrayList.get(position).isSelected) {
            viewHolder.selectedIv.setImageResource(R.drawable.dir_choose);
            viewHolder.selectedIv.setVisibility(View.VISIBLE);
        } else {
            viewHolder.selectedIv.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    private class BankViewHolder {
        private TextView bankName, accountName;
        private ImageView selectedIv;
    }
}
